package com.suxin.shiro.md5;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;

/**
 * @author Tang
 * @classname MainTest
 * @description [  ]
 * @date 2021/3/20 13:30
 */
public class MainTest {

    @RequiresUser()
    public static void main(String[] args) {

        DefaultSecurityManager manager = new DefaultSecurityManager();

        MyRealm myRealm = new MyRealm();
        // md5算法
        HashedCredentialsMatcher matcher = new HashedCredentialsMatcher("MD5");
        matcher.setHashIterations(1024);
        myRealm.setCredentialsMatcher(matcher);
        manager.setRealm(myRealm);

        SecurityUtils.setSecurityManager(manager);

        Subject subject = SecurityUtils.getSubject();

        try {
            System.out.println("是否认证：" + subject.isAuthenticated());
            subject.login(new UsernamePasswordToken("name","long"));
            System.out.println("是否认证：" + subject.isAuthenticated());
        } catch (Exception e) {
            System.err.println("认证失败....");
        }

        if (subject.isAuthenticated()) {
            if (subject.hasRole("admin")) {
                // 有权限
                System.out.println("当前有admin权限...");
            }
            if (subject.isPermitted("user:update:002")) {
                System.out.println("当前可以更新用户001的记录...");
            }
        }

    }

}